package com.ruoyi.coupon.service.impl;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.coupon.domain.CouponConfig;
import com.ruoyi.coupon.mapper.CouponConfigMapper;
import com.ruoyi.coupon.service.ICouponConfigService;
import com.ruoyi.coupon.vo.AddUserCouponVo;
import com.ruoyi.coupon.vo.SelectUserCouponVo;
import com.ruoyi.user.domain.UserCoupon;
import com.ruoyi.user.mapper.UserCouponMapper;
import com.ruoyi.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 卡券管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@Service
public class CouponConfigServiceImpl implements ICouponConfigService {
    @Autowired
    private CouponConfigMapper couponConfigMapper;
    @Resource
    private UserCouponMapper userCouponMapper;
    @Resource
    private UserMapper userMapper;

    /**
     * 查询卡券管理
     *
     * @param id 卡券管理主键
     * @return 卡券管理
     */
    @Override
    public CouponConfig selectCouponConfigById(Long id) {
        return couponConfigMapper.selectCouponConfigById(id);
    }

    /**
     * 查询卡券管理列表
     *
     * @param couponConfig 卡券管理
     * @return 卡券管理
     */
    @Override
    public List<CouponConfig> selectCouponConfigList(CouponConfig couponConfig) {

        return couponConfigMapper.selectCouponConfigList(couponConfig);
    }

    /**
     * 新增卡券管理
     *
     * @param couponConfig 卡券管理
     * @return 结果
     */
    @Override
    public int insertCouponConfig(CouponConfig couponConfig) {
        couponConfig.setCreateTime(DateUtils.getNowDate());
        return couponConfigMapper.insertCouponConfig(couponConfig);
    }

    /**
     * 修改卡券管理
     *
     * @param couponConfig 卡券管理
     * @return 结果
     */
    @Override
    public int updateCouponConfig(CouponConfig couponConfig) {
        couponConfig.setUpdateTime(DateUtils.getNowDate());
        return couponConfigMapper.updateCouponConfig(couponConfig);
    }

    /**
     * 批量删除卡券管理
     *
     * @param ids 需要删除的卡券管理主键
     * @return 结果
     */
    @Override
    public int deleteCouponConfigByIds(Long[] ids) {
        return couponConfigMapper.deleteCouponConfigByIds(ids);
    }

    /**
     * 删除卡券管理信息
     *
     * @param id 卡券管理主键
     * @return 结果
     */
    @Override
    public int deleteCouponConfigById(Long id) {
        return couponConfigMapper.deleteCouponConfigById(id);
    }

    @Override
    public UserCoupon selectUserCoupon(SelectUserCouponVo requestVo) {
        CouponConfig couponConfig = couponConfigMapper.selectCouponConfigById(requestVo.getId());
        if (CheckEmptyUtil.isEmpty(couponConfig)) {
            throw new RuntimeException("卡券不存在");
        }
        UserCoupon userCoupon = new UserCoupon();
        String tName = userMapper.selectTName(couponConfig.getCouponScopeApplication());
        userCoupon.setTechnicianName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
        userCoupon.setCouponName(couponConfig.getCouponName());
        userCoupon.setCouponId(couponConfig.getId());
        userCoupon.setCouponStatus("NOT");
        userCoupon.setCoupomScopeApplication(Math.toIntExact(couponConfig.getCouponScopeApplication()));
        userCoupon.setCloseTime(DateUtil.offsetDay(new Date(), couponConfig.getEffectDay()));
        userCoupon.setCouponType(0);
        userCoupon.setCouponFull(couponConfig.getCouponFull());
        userCoupon.setCouponSubtract(couponConfig.getCouponSubtract());
        return userCoupon;
    }

    @Override
    public Integer addUserCoupon(AddUserCouponVo requestVo) {
        int inserted = 0;
        for (int i = 0; i < requestVo.getCount(); i++) {
            UserCoupon userCoupon = new UserCoupon();
            userCoupon.setUserId(requestVo.getUserId());
            userCoupon.setCouponId(requestVo.getCouponId());
            userCoupon.setCouponStatus(requestVo.getCouponStatus());
            userCoupon.setCoupomScopeApplication(requestVo.getCoupomScopeApplication());
            userCoupon.setCloseTime(requestVo.getCloseTime());
            userCoupon.setCouponType(0);
            userCoupon.setCouponFull(requestVo.getCouponFull());
            userCoupon.setCouponSubtract(requestVo.getCouponSubtract());
            userCoupon.setCreateTime(DateUtils.getNowDate());
             inserted = userCouponMapper.insertUserCoupon(userCoupon);
        }

        return inserted;
    }
}
